<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="1. 尾部的0设计一个算法，计算出n阶乘中尾部零的个数 思路：数据中如果有5的倍数的话就多一个0,25的倍数话多两个0,但是注意了如果通用算了5的倍数的时候其实也算了25的倍数所以思路很简单，就是算算到n为止有多少5,25,125…个倍数就行了 123456789101112public class Solution &amp;#123;    /*     * @param n: An integer">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/算法阶段性小结(一)/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="1. 尾部的0设计一个算法，计算出n阶乘中尾部零的个数 思路：数据中如果有5的倍数的话就多一个0,25的倍数话多两个0,但是注意了如果通用算了5的倍数的时候其实也算了25的倍数所以思路很简单，就是算算到n为止有多少5,25,125…个倍数就行了 123456789101112public class Solution &amp;#123;    /*     * @param n: An integer">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.341Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="1. 尾部的0设计一个算法，计算出n阶乘中尾部零的个数 思路：数据中如果有5的倍数的话就多一个0,25的倍数话多两个0,但是注意了如果通用算了5的倍数的时候其实也算了25的倍数所以思路很简单，就是算算到n为止有多少5,25,125…个倍数就行了 123456789101112public class Solution &amp;#123;    /*     * @param n: An integer">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/算法阶段性小结(一)/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/算法阶段性小结(一)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="1-尾部的0"><a href="#1-尾部的0" class="headerlink" title="1. 尾部的0"></a>1. 尾部的0</h3><p>设计一个算法，计算出n阶乘中尾部零的个数</p>
<p>思路：数据中如果有5的倍数的话就多一个0,25的倍数话多两个0,但是注意了如果通用算了5的倍数的时候其实也算了25的倍数所以思路很简单，就是算算到n为止有多少5,25,125…个倍数就行了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * @param n: An integer</span></span><br><span class="line"><span class="comment">     * @return: An integer, denote the number of trailing zeros in n!</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">trailingZeros</span><span class="params">(<span class="keyword">long</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> count=<span class="number">0</span>;<span class="comment">//一定是long~~~~</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;Math.pow(<span class="number">5</span>,i)&lt;=n;i++)</span><br><span class="line">            count+=n/(<span class="keyword">long</span>)Math.pow(<span class="number">5</span>,i);</span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>反思，好久没有动脑了，只是看到了表面的东西，没有深入思考，要恢复以前对数字敏感的状态</p>
<h3 id="2-提及数字"><a href="#2-提及数字" class="headerlink" title="2. 提及数字"></a>2. 提及数字</h3><p>计算数字k在0到n中的出现的次数，k可能是0~9的一个数值</p>
<p>基本的排列组合 比如 123 这个数组要算 1出现的个数 就算 ××1 ×1× 1×× 分别有多少个就可以了，但是又很多的边界问题比如</p>
<p>比如：001可以存在，但是如果这个数组只有一位比如 9 那么就不能有 01  </p>
<p>验证一下为什么这么做对，因为 在计算××1 的时候 想 ×11 和 111 这种情况只计算了一次，所以再计算×1× 和 1×× 的时候就不需要去重了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Code2</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Code2 main2 = <span class="keyword">new</span> Code2();</span><br><span class="line">        <span class="keyword">long</span> nowTime = <span class="keyword">new</span> Date().getTime();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">10000</span>; i &lt; <span class="number">99999</span>; i++) &#123;</span><br><span class="line">            main2.digitCounts3(<span class="number">2</span>,i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">long</span> endTime = <span class="keyword">new</span> Date().getTime();</span><br><span class="line">        System.out.println(endTime - nowTime);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 复杂做法，使用计算法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">digitCounts</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> item;</span><br><span class="line">        <span class="keyword">int</span> beforeNumber;</span><br><span class="line">        <span class="keyword">int</span> base = <span class="number">10</span>;</span><br><span class="line">        <span class="keyword">int</span> all = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">do</span> &#123;</span><br><span class="line">            beforeNumber = n / base * base;</span><br><span class="line">            item = (n - beforeNumber) / (base / <span class="number">10</span>);</span><br><span class="line">            <span class="keyword">if</span> (item &gt; k) &#123;</span><br><span class="line">                all += (n / base + <span class="number">1</span>) * (base / <span class="number">10</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (item == k) &#123;</span><br><span class="line">                all += (n / base) * (base / <span class="number">10</span>) + (n % (base / <span class="number">10</span>) + <span class="number">1</span>);<span class="comment">// 1 12 这种情况下注意有全零的数字   21345这个数据</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                all += (n / base) * (base / <span class="number">10</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            base *= <span class="number">10</span>;</span><br><span class="line">        &#125; <span class="keyword">while</span> (beforeNumber != <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">if</span> (k == <span class="number">0</span> &amp;&amp; base &gt; <span class="number">100</span>) &#123;<span class="comment">//01 02 不算数</span></span><br><span class="line">            all -= base / <span class="number">100</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> all;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//简单暴力方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">digitCounts3</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        <span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=n;i++)&#123;</span><br><span class="line">            <span class="keyword">int</span> num=i;</span><br><span class="line">            <span class="keyword">while</span>(num/<span class="number">10</span>!=<span class="number">0</span>)&#123;</span><br><span class="line">                <span class="keyword">if</span>(num%<span class="number">10</span>==k)&#123;</span><br><span class="line">                    sum++;</span><br><span class="line">                &#125;</span><br><span class="line">                num=num/<span class="number">10</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(num==k)</span><br><span class="line">                sum++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>反思：思路要灵活重要的是脑子的反应要迅速，这道题目不难要 注意边界问题，为什么这么做正确的原因区间冗余是非常重要的</p>
<h3 id="3-丑数"><a href="#3-丑数" class="headerlink" title="3. 丑数"></a>3. 丑数</h3><p>设计一个算法，找出只含素因子2，3，5 的第 n 小的数。</p>
<p>符合条件的数如：1, 2, 3, 4, 5, 6, 8, 9, 10, 12…</p>
<p>这道题使用递推的思想最好，高级递推（有状态的递推）</p>
<p>有题目可以知道的  最开始我的思路一个丑数×2,×3，×5，分别得到下一个但是发现这样其实是错误的，比如3×3&lt;2*5,那么10会排在9的前面，所以我们要记录这个丑数被谁乘过，然后从中挑选出最小的一个</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LeetLintCode01_</span>丑数 </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Code4 code4 = <span class="keyword">new</span> Code4();</span><br><span class="line">        System.out.println(code4.nthUglyNumber3(<span class="number">100</span>));</span><br><span class="line">        System.out.println(<span class="keyword">new</span> LeetLintCode01_丑数().nthSuperUglyNumber(<span class="number">6</span>,<span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">2</span>&#125;));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">nthSuperUglyNumber</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span>[] primes)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        <span class="keyword">int</span>[] ugarr = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line">        ugarr[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span>[] item = <span class="keyword">new</span> <span class="keyword">int</span>[primes.length];<span class="comment">//分别对应的2,3,5 的被乘数的下标</span></span><br><span class="line"><span class="comment">//        int[] itemx= new int[3];//分别对应2,3,5 的乘数</span></span><br><span class="line"><span class="comment">//        itemx[0]=2;</span></span><br><span class="line"><span class="comment">//        itemx[1]=3;</span></span><br><span class="line"><span class="comment">//        itemx[2]=5;</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> a = <span class="number">1</span>; a &lt; ugarr.length; a++) &#123;</span><br><span class="line">            <span class="keyword">int</span> index=<span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> b = <span class="number">0</span>; b &lt; primes.length; b++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (ugarr[item[b]]*primes[b]&lt;ugarr[item[index]]*primes[index])&#123;</span><br><span class="line">                    index=b;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> b = <span class="number">0</span>; b &lt;  primes.length; b++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (b!=index&amp;&amp;ugarr[item[b]]*primes[b]==ugarr[item[index]]*primes[index])&#123;</span><br><span class="line">                    item[b]++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            ugarr[a]=ugarr[item[index]]*primes[index];</span><br><span class="line">            item[index]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ugarr[n - <span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-第k大元素"><a href="#4-第k大元素" class="headerlink" title="4. 第k大元素"></a>4. 第k大元素</h3><p>在数组中找到第k大的元素，很简单，快速排序的应用，每一次查拆分其实是将数据进行了一次分类，可以归纳到，分治，递归的思想上，主要是要注意使用快排的时候的边界问题</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Code5</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">kthLargestElement</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">int</span>[] nums)</span> </span>&#123;</span><br><span class="line">        k = k - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> index = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> b = nums.length - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> start = a;</span><br><span class="line">            <span class="keyword">int</span> end = b;</span><br><span class="line">            index = nums[start];</span><br><span class="line">            <span class="keyword">while</span> (start != end) &#123;</span><br><span class="line">                <span class="keyword">while</span> (nums[end] &lt; index &amp;&amp; start != end) &#123;</span><br><span class="line">                    end--;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (start != end) &#123;</span><br><span class="line">                    nums[start] = nums[end];</span><br><span class="line">                    start++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">while</span> (nums[start] &gt; index &amp;&amp; start != end) &#123;</span><br><span class="line">                    start++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (start != end) &#123;</span><br><span class="line">                    nums[end] = nums[start];</span><br><span class="line">                    end--;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            nums[start] = index;</span><br><span class="line">            <span class="keyword">if</span> (start &gt; k) &#123;</span><br><span class="line">                b = start - <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (start &lt; k) &#123;</span><br><span class="line">                a = start + <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> nums[start];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="5-旋转字符串"><a href="#5-旋转字符串" class="headerlink" title="5. 旋转字符串"></a>5. 旋转字符串</h3><p>给定一个字符串和一个偏移量，根据偏移量旋转字符串(从左向右旋转),这个问题关键是截枝，一个字符串asd 其实反转 asd.length步就变回原来的了，所以没必要就移动n步，这个地方坑住我了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Arrays;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> str:    An array of char</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> offset: An integer</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span>: nothing</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">rotateString</span><span class="params">(<span class="keyword">char</span>[] str, <span class="keyword">int</span> offset)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (str.length == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        offset = offset % str.length;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        <span class="keyword">while</span> (offset &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">char</span> end = str[str.length - <span class="number">1</span>];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> a = str.length - <span class="number">1</span>; a &gt; <span class="number">0</span>; a--) &#123;</span><br><span class="line">                str[a] = str[a - <span class="number">1</span>];</span><br><span class="line">            &#125;</span><br><span class="line">            str[<span class="number">0</span>] = end;</span><br><span class="line">            offset--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="6-带最小值操作的栈"><a href="#6-带最小值操作的栈" class="headerlink" title="6. 带最小值操作的栈"></a>6. 带最小值操作的栈</h3><p>实现一个带有取最小值min方法的栈，min方法将返回当前栈中的最小值。</p>
<p>你实现的栈将支持push，pop 和 min 操作，所有操作要求都在O(1)时间内完成。</p>
<p>这个问题其实就是在push的时候每次都记住min的直并且每次pop操作动态更新就好了</p>
<p>收获主要就是不能单单的看到表象，更应该深入的数据中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MinStack</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Deque&lt;Integer&gt; mq = <span class="keyword">new</span> LinkedList&lt;&gt;();</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Deque&lt;Integer&gt; min = <span class="keyword">new</span> LinkedList&lt;&gt;();</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MinStack</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// do intialization if necessary</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * @param number: An integer</span></span><br><span class="line"><span class="comment">     * @return: nothing</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">push</span><span class="params">(<span class="keyword">int</span> number)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        <span class="keyword">if</span> (min.size() == <span class="number">0</span> || number &lt; min.getLast()) &#123;</span><br><span class="line">            min.addLast(number);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            min.addLast(min.getLast());</span><br><span class="line">        &#125;</span><br><span class="line">        mq.addLast(number);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * @return: An integer</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">pop</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        min.removeLast();</span><br><span class="line">        <span class="keyword">return</span> mq.removeLast();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * @return: An integer</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">min</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        <span class="keyword">return</span> min.getLast();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="7-骰子求和"><a href="#7-骰子求和" class="headerlink" title="7. 骰子求和"></a>7. 骰子求和</h3><p>扔 n 个骰子，向上面的数字之和为 S。给定 Given n，请列出所有可能的 S 值及其相应的概率。</p>
<p>这个排列组合和 <strong>提及数字计算数字k在0到n中的出现的次数，k可能是0~9的一个数值</strong> 很像，这题的思路就是通过投掷两个骰子去推算三个骰子的情况，和2题相同的思想，排列组合的区间覆盖，（梯度的覆盖，慢慢回想）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> List&lt;Map.Entry&lt;Integer, Double&gt;&gt; dicesSum(<span class="keyword">int</span> n) &#123;</span><br><span class="line">        <span class="keyword">double</span>[] dp = <span class="keyword">new</span> <span class="keyword">double</span>[n * <span class="number">6</span> + <span class="number">1</span>];</span><br><span class="line">        dp[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> a = <span class="number">1</span>; a &lt;= n; a++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> b = a * <span class="number">6</span>; b &gt;= a; b--) &#123;</span><br><span class="line">                <span class="keyword">int</span> start = <span class="number">1</span>;</span><br><span class="line">                start = b - <span class="number">6</span> &gt;= a-<span class="number">1</span> ? b - <span class="number">6</span> : a-<span class="number">1</span>;</span><br><span class="line">                <span class="keyword">int</span> end = start+<span class="number">5</span>&gt;(a-<span class="number">1</span>)*<span class="number">6</span>?(a-<span class="number">1</span>)*<span class="number">6</span>:start+<span class="number">5</span>&gt;=b?b-<span class="number">1</span>:start+<span class="number">5</span>;</span><br><span class="line">                <span class="keyword">double</span> all = <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">while</span> (start &lt;= end) &#123;</span><br><span class="line">                    all += dp[start];</span><br><span class="line">                    start++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (a != <span class="number">1</span>) &#123;</span><br><span class="line">                    dp[b] = all;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    dp[b] = <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        ArrayList&lt;Map.Entry&lt;Integer, Double&gt;&gt; needlist = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">double</span> all= Math.pow(<span class="number">6</span>, n);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> a = n; a &lt;= <span class="number">6</span> * n; a++) &#123;</span><br><span class="line">            AbstractMap.SimpleEntry&lt;Integer, Double&gt; entry = <span class="keyword">new</span> AbstractMap.SimpleEntry&lt;Integer, Double&gt;(a, dp[a] /all);</span><br><span class="line">            needlist.add(entry);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> needlist;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="8-最大子数组"><a href="#8-最大子数组" class="headerlink" title="8. 最大子数组"></a>8. 最大子数组</h3><p>没什么好说的高级DP，max[k个数组][n列]=max(max[k][n-1],max[k-1][n-1]+item[n])</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> nums: A list of integers</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> k: An integer denote to find k non-overlapping subarrays</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span>: An integer denote the sum of max k non-overlapping subarrays</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">maxSubArray</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// write your code here</span></span><br><span class="line">        <span class="keyword">int</span>[][] singleMax = <span class="keyword">new</span> <span class="keyword">int</span>[k + <span class="number">1</span>][nums.length + <span class="number">1</span>];</span><br><span class="line"><span class="comment">//        表示j个数，i个子数组的最大和，必须包含第j个数</span></span><br><span class="line">        <span class="keyword">int</span>[][] max = <span class="keyword">new</span> <span class="keyword">int</span>[k + <span class="number">1</span>][nums.length + <span class="number">1</span>];</span><br><span class="line"><span class="comment">//        表示j个数，i个子数组的最大和，可以不包含第j个数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= k; i++) &#123;</span><br><span class="line"><span class="comment">//            要分成i个子数组，最小得有i个数</span></span><br><span class="line">            singleMax[i][i - <span class="number">1</span>] = Integer.MIN_VALUE;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i; j &lt;= nums.length; j++) &#123;</span><br><span class="line"><span class="comment">//                包含第j个数的最大值，是它和前边一起组成始数组或者它单独成为子数组的大的那个</span></span><br><span class="line">                singleMax[i][j] = Math.max(singleMax[i][j - <span class="number">1</span>], max[i - <span class="number">1</span>][j - <span class="number">1</span>]) + nums[j - <span class="number">1</span>];</span><br><span class="line">                <span class="keyword">if</span> (i == j) &#123;</span><br><span class="line">                    max[i][j] = singleMax[i][j];</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    max[i][j] = Math.max(singleMax[i][j], max[i][j - <span class="number">1</span>]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> max[k][nums.length];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/详解http协议-基础概念/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/深入理解mybatis（二）映射文件/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-尾部的0"><span class="nav-number">1.</span> <span class="nav-text">1. 尾部的0</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-提及数字"><span class="nav-number">2.</span> <span class="nav-text">2. 提及数字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-丑数"><span class="nav-number">3.</span> <span class="nav-text">3. 丑数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-第k大元素"><span class="nav-number">4.</span> <span class="nav-text">4. 第k大元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-旋转字符串"><span class="nav-number">5.</span> <span class="nav-text">5. 旋转字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-带最小值操作的栈"><span class="nav-number">6.</span> <span class="nav-text">6. 带最小值操作的栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-骰子求和"><span class="nav-number">7.</span> <span class="nav-text">7. 骰子求和</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-最大子数组"><span class="nav-number">8.</span> <span class="nav-text">8. 最大子数组</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
